<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="com.ld.xuanKe.service.CourseService" %>
<%@ page import="com.ld.xuanKe.service.impl.CourseServiceImpl" %>
<%@ page import="com.ld.xuanKe.entity.Course" %>
<%@ page import="java.util.List" %>
<%@ page import="com.ld.xuanKe.entity.User" %>
<%@ page import="org.springframework.context.ApplicationContext" %>
<%@ page import="static org.apache.struts2.ServletActionContext.getServletContext" %>
<%@ page import="org.springframework.web.context.WebApplicationContext" %><%--
  Created by IntelliJ IDEA.
  User: 刘邓
  Date: 2023-06-10
  Time: 16:13
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>课程管理</title>
    <link rel="stylesheet" type="text/css" href="/common.css"/>
</head>
<body>

<%
    ApplicationContext applicationContext = (ApplicationContext) getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    CourseService courseService = (CourseServiceImpl) applicationContext.getBean(CourseService.class);
    String name = request.getParameter("name");
    String valid = request.getParameter("valid");
    List<Course> courses;
    if (name == null) {
        courses = courseService.list();
    } else {
        courses = courseService.getCoursesByLikeName(name);
    }
    if (valid == null) {
        courses = courseService.list();
    } else {
        courses = courseService.getValidCourses();
    }
    User user = (User) (session.getAttribute("user"));
    String homePath;
    if (user.getStatus() == 0) {
        homePath = "user";
    } else {
        homePath = "admin";
    }
%>
<a href="/home/<%=homePath%>/index.jsp"
   style="display: inline-block;height: 30px;margin:10px 30px;text-align:center;line-height:30px;background-color:#DDD;text-decoration: none;">返回个人管理页面</a>
<h3>选课管理说明</h3>
<ul class='explain'>
    <li>选课开始时间为空，结束时间不为空，表示现在的时间小于或等于结束时间就可以选课</li>
    <li>选课开始时间不为空，结束时间为空，表示现在的时间大于或等于开始时间就可以选课</li>
    <li>一键删除无效课程,表示选课结束删除且已选人数小于40人的课程</li>
</ul>
<form id='like' action='/home/admin/course.jsp' method='get'><input name='name'>&nbsp&nbsp<input type='submit'
                                                                                                 value='按课程名模糊查询'>
</form>
<div class='navigate'><a href='/home/admin/addCourse.jsp'>添加课程</a>
    <a href='/course/downLoad'>导出为excel</a>
    <a href='/course/template'>下载导入excel模板</a>
    <a href='/home/admin/upLoadCourseExcel.jsp'>导入excel</a>
    <a href='/home/admin/unifySelect.jsp'>统一选课</a>
    <a href='/home/admin/course.jsp?valid=1'>一键查看有效课程</a>
    <a href='/teachingClass/add'>生成教学班</a>
    <a href='/course/deleteNotValid' class='deleteValid'>一键删除无效课程</a>
</div>
<c:if test="<%=courses.isEmpty()%>">
    <h1 style='width: 80%;height:400px;margin: 0 auto;text-align:center;line-height:400px'>暂无更多信息!</h1>
</c:if>
<c:if test="<%=!courses.isEmpty()%>">
    <table id='all-request' class='container'>

        <thead>
        <tr>
            <th>课程id</th>
            <th>课程名</th>
            <th>授课老师</th>
            <th>容量</th>
            <th>已选人数</th>
            <th>选课开始时间</th>
            <th>选课结束时间</th>
            <th>课程说明</th>
            <th>学分</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <c:forEach items="<%=courses%>" var="course">
            <tr>
                <td>${course.getId() }</td>
                <td>${course.getName() }</td>
                <td>${course.getTeacher() }</td>
                <td>${course.getCapacity() }</td>
                <td>${course.getSelected() }</td>
                <td>${course.getStartTime() }</td>
                <td>${course.getEndTime() }</td>
                <td>${course.getBz() }</td>
                <td>${course.getXf() }</td>
                <td class='accept'><a href=" /home/admin/changeCourse.jsp?id=${course.getId()}">修改</a></td>
                <td class='accept toDelete'><a class='deleted' href="/course/delete?id=${course.getId()}">删除</a></td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
</c:if>

</body>
<script>
    let deleted = document.getElementsByClassName("deleted");
    for (let i = 0; i < deleted.length; i++) {
        deleted[i].addEventListener("click", (e) => {
            let b = confirm("你确定要彻底删除吗?");
            if (b == false) {
                e.preventDefault();
            }
        })
    }
    let deleteValid = document.getElementsByClassName("deleteValid");
    deleteValid[0].addEventListener('click', (e) => {
        let b = confirm("你确定要删除选课结束后已选人数小于40的课程和所有已选这门课的人关于这门课的信息吗?");
        if (b == false) {
            e.preventDefault();
        }
    })
</script>
</html>
